<?php

namespace app\sys\controller;

use think\Db;

// 后台首页
class Index extends Admin
{
    public function index()
    {
        $admin_info = session('admin_info');
        if (6 == $admin_info['permission']) {
            $query = [];
            // 供应商只能看到自己的
            $query['admin_id'] = $admin_info['id'];
            $query['status'] = 1;
            $count['count_1'] = Db::name('order')->where($query)->count();
            $query['status'] = 2;
            $count['count_2'] = Db::name('order')->where($query)->count();
            $query['status'] = 5;
            $count['count_3'] = Db::name('order')->where($query)->count();
            $query['status'] = 6;
            $count['count_4'] = Db::name('order')->where($query)->count();
            $this->assign('count', $count);

            return view('index');
        } elseif (10 == $admin_info['permission']) {
            // 加缓存
            $total_data = cache('admin_total_data');
            if (!$total_data) {
                $total_data['count_user'] = Db::name('user')->count();
                $total_data['count_user_member'] = Db::name('user')->where('gang_role', 2)->count();
                $total_data['count_order'] = Db::name('order')->count();
                $total_data['count_order_deal'] = Db::name('order')->where('status', 10)->count();
                $total_data['count_gang'] = Db::name('gang')->count();
                $count_gang_max = Db::name('gang')->order(['member_num' => -1])->limit(1)->select();
                $total_data['count_gang_max'] = $count_gang_max[0]['member_num'];
                $total_data['count_red_packet'] = Db::name('red_packet')->count();
                $total_data['count_red_packet_not_open'] = Db::name('red_packet')->where('not_open', 1)->count();
                $total_data['count_gzh_user'] = Db::name('gzh_user')->count();
                $total_data['count_gzh_user_women'] = Db::name('gzh_user')->where('sex', 2)->count();
                // $total_data['count_product'] = Db::name('product')->count();
                // $total_data['count_product_sold_out'] = Db::name('product')->where('sold_out', 1)->count();
                $bt=mktime(0,0,0,date('m'),date('d')-30,date('Y'));
                $et=mktime(23,59,59,date('m'),date('d'),date('Y')); 
                $r = Db::name('total_data')->where(['ct' => ['lt', $bt]])->aggregate(['_id' => null, 'user_num' => ['$sum' => '$user_num'], 'red_packet' => ['$sum' => '$red_packet'], 'red_packet_money' => ['$sum' => '$red_packet_money']]); 
                $total_list = Db::name('total_data')->where(['ct' => [['gte', $bt], ['lte', $et]]])->order(['ct'=>1])->select();
                $date = $user_num = $red_packet = $red_packet_money = [];
                $total_user_num = $total_red_packet = $total_red_packet_money = [];
                $t_user_num = intval($r[0]['user_num']);
                $t_red_packet = intval($r[0]['t_red_packet']);
                $t_red_packet_money = intval($r[0]['t_red_packet_money']); 
                foreach($total_list as $data){
                    $date[] = date('m-d',$data['ct']);
                    $user_num[] = intval($data['user_num']);
                    $red_packet[] = intval($data['red_packet']);
                    $red_packet_money[] = round($data['red_packet_money'],2);
                    $t_user_num += intval($data['user_num']);
                    $t_red_packet += intval($data['red_packet']);
                    $t_red_packet_money += round($data['red_packet_money'],2);
                    $total_user_num[] = intval($t_user_num);
                    $total_red_packet[] = intval($t_red_packet);
                    $total_red_packet_money[] = round($t_red_packet_money,2);
                }
                $total_data['date'] = json_encode($date);
                $total_data['user_num'] = json_encode($user_num);
                $total_data['red_packet'] = json_encode($red_packet);
                $total_data['red_packet_money'] = json_encode($red_packet_money);  
                $total_data['total_user_num'] = json_encode($total_user_num);
                $total_data['total_red_packet'] = json_encode($total_red_packet);
                $total_data['total_red_packet_money'] = json_encode($total_red_packet_money);  
                
                cache('admin_total_data', $total_data, 120);
            }
            $total_data['loadavg'] = sys_getloadavg();

            $this->assign('total_data', $total_data);

            return view('adminIndex');
        }

    }

    public function noaccess()
    {
        return view('noaccess');
    }

    public function four0four()
    {
        return view('four0four');
    }

    public function login()
    {
        // 本来想采用jwt的 但是赶时间 没有做前后分离 用session比较快
        // github.com/firebase/php-jwt
        if (request()->isPost()) {
            $username = input('post.username', '');
            $password = input('post.password', '');
            if (!$username) {
                return error('参数错误：错误的username');
            }
            if (!$password) {
                return error('参数错误：错误的password');
            }
            $password = md5($password.config('api_secret'));
            $admin_info = Db::name('admin')->where(['username|phone' => $username, 'password' => $password])->find();
            if (!$admin_info) {
                return error('账号或密码错误');
            }
            unset($admin_info['password']);
            session('admin_info', $admin_info);

            return success(session('redirect_url'));
        }

        return view('login');
    }

    public function loginOut()
    {
        session('admin_info', null);

        return $this->redirect('index/login');
    }

    // 生成管理员扫码登录的二维码（用来绑定微信公众号的）
    public function getQrcode()
    {
        $id = input('param.admin_id', '');
        $type = input('param.type', '1');
        $wxs = new \wx\WeixinService();
        $path = 'pages/index/welcome';
        $result = $wxs->getQrCodeUnlimit($path, 325, $id ? $id.'_'.$type : strRand().'_2');
        header('Content-type: image/png');
        echo $result;
        exit;
    }

    // 生成业务员 邀请创建帮会的二维码
    public function getJoinQrcode()
    {
        $csid = input('param.csid', '');
        if (!$csid) {
            return error('参数错误：缺少csid');
        }
        $wxs = new \wx\WeixinService();
        $path = 'pages/gangManage/create/create';
        $result = $wxs->getQrCodeUnlimit($path, 325, $csid);
        header('Content-type: image/png');
        echo $result;
        exit;
    }

    // 生成 会长邀请码
    public function getJoinGangQrcode()
    {
        $user_id = input('param.user_id', '');
        if (!$user_id) {
            return error('参数错误：缺少user_id');
        }
        $wxs = new \wx\WeixinService();
        $path = 'pages/banghui/join';
        $result = $wxs->getQrCodeUnlimit($path, 325, $user_id);
        header('Content-type: image/png');
        echo $result;
        exit;
    }

    // 获取登录结果
    public function getWxLogin()
    {
        if (request()->isPost()) {
            $id = input('param.admin_id', '');
            $login_info = cache('admin_wx_login_'.$id);
            if (!$login_info) {
                return error('未登录成功');
            }
            cache('admin_wx_login_', null);

            return success($login_info);
        }

        return error('错误的请求');
    }
}
